package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Xutao Bai
 * @Date created in 2020/1/3
 * @description
 * @Version 1.0
 */

@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> findAllList() {
        return menuDao.selectAllList();
    }

    @Override
    public Menu findById(Integer id) {
        log.info("[菜单_根据id查询]date:{}", id);
        return menuDao.selectById(id);

    }

    /**
     * 添加所有 菜单项
     *
     * @param menu
     */
    @Transactional
    @Override
    public void add(Menu menu) {
        log.info("[菜单-添加]data:{}", menu);
        menuDao.insert(menu);
    }

    /**
     * 根据ParentMenuId 查询所有菜单项
     *
     * @return
     */
    @Override
    public Menu findByParentMenuId(Integer ParentMenuId) {
        log.info("[菜单_查询-根据ParentMenuId]date:{}", ParentMenuId);

        return menuDao.selectByParentMenuId(ParentMenuId);
    }

    /**
     * 菜单-查询所有
     *
     * @return
     */
    @Override
    public List<Menu> findAll() {

        return menuDao.selectAll();
    }

    /**
     * 菜单-分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[菜单_分页查询]date:{}", queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //分页查询数据库
        Page<Menu> page = menuDao.selectByConditon(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据菜单id删除
     *
     * @param id
     */
    @Transactional
    @Override
    public void delete(Integer id) {
        log.info("[菜单-根据id删除]data:{}", id);
        menuDao.deleteById(id);
    }

    @Override
    public Integer count2MenuByPath(String path) {
        Integer count = menuDao.count2MenuByPath(path);
        return  count;
    }

    @Override
    public Integer findchildrenByMenuId(Integer id) {
        return menuDao.selectchildrenByMenuId(id);
    }
}
